﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Agen.ServiceAgencyWPF;
using System.Collections.ObjectModel;
using Agen.Models;
using System.ComponentModel;

namespace Agen.Views
{
    public partial class OfficialExam : Page
    {

        private OfficialExamVO vo;

        public OfficialExam()
        {
            InitializeComponent();
            vo = new OfficialExamVO();
            DataContext = vo;
            ApplicationManager.client.GetExamsByCustomerIDCompleted+=new EventHandler<GetExamsByCustomerIDCompletedEventArgs>(client_GetExamsByCustomerIDCompleted);
            ApplicationManager.client.GetAllExamsAgencyByCustomerCompleted += new EventHandler<GetAllExamsAgencyByCustomerCompletedEventArgs>(client_GetAllExamsAgencyByCustomerCompleted);
            ApplicationManager.client.DeleteExamToCustomerCompleted+=new EventHandler<DeleteExamToCustomerCompletedEventArgs>(client_DeleteExamToCustomerCompleted);
            ApplicationManager.client.AddExamToUserCompleted += new EventHandler<AddExamToUserCompletedEventArgs>(client_AddExamToUserCompleted);
        }

        void client_AddExamToUserCompleted(object sender, AddExamToUserCompletedEventArgs e)
        {
            if (e.Result)
            {
                refreshLists();
            }
        }

        void client_GetAllExamsAgencyByCustomerCompleted(object sender, GetAllExamsAgencyByCustomerCompletedEventArgs e)
        {
            vo.ListExams = e.Result;
        }

        void client_DeleteExamToCustomerCompleted(object sender, DeleteExamToCustomerCompletedEventArgs e)
        {
            if(e.Result != 0){
                refreshLists();
            }
        }

        void client_GetExamsByCustomerIDCompleted(object sender, GetExamsByCustomerIDCompletedEventArgs e)
        {
            vo.ListCustomerExams = e.Result;
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
             refreshLists();
        }


        private void addCustomer_btn_Click(object sender, RoutedEventArgs e)
        {
            ExamsByCustomer_Result exam = (sender as Button).Tag as ExamsByCustomer_Result;
            ApplicationManager.client.AddExamToUserAsync(exam.IdOfficialTest, ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
        }

        private void deleteExam_btn_Click(object sender, RoutedEventArgs e)
        {
            OfficialTestDC exam = (sender as Button).Tag as OfficialTestDC;
            ApplicationManager.client.DeleteExamToCustomerAsync(exam.Id, ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
        }
        private void refreshLists() {
            ApplicationManager.client.GetAllExamsAgencyByCustomerAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
            ApplicationManager.client.GetExamsByCustomerIDAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
        }

        private void cancel_button_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.FrameAction(this, ApplicationManager.PREVIOUS_HANDLE, new Object());
        }
    }



    public class OfficialExamVO : INotifyPropertyChanged
    {

        private OfficialTestDC _officialExam; 
        private ObservableCollection<ExamsByCustomer_Result> _listExams;
        private ObservableCollection<OfficialTestDC> _listCustomerExams;

        public OfficialExamVO()
        {
            this.OfficialExam = new OfficialTestDC();
            this.ListExams = new ObservableCollection<ExamsByCustomer_Result>();
            this.ListCustomerExams = new ObservableCollection<OfficialTestDC>(); 

        }

        public void refreshOfficialExam()
        {
            this.OfficialExam = new OfficialTestDC();
        }

        public OfficialTestDC OfficialExam
        {
            get
            {
                return this._officialExam;
            }
            set
            {
                this._officialExam = value;
                OnPropertyChanged("OfficialExam");
            } 
        }

        public ObservableCollection<ExamsByCustomer_Result> ListExams
        {
            get
            {
                return this._listExams;
            }
            set
            {
                this._listExams = value;
                OnPropertyChanged("ListExams");
            } 
        }
        public ObservableCollection<OfficialTestDC> ListCustomerExams
        {
            get
            {
                return this._listCustomerExams;
            }
            set
            {
                this._listCustomerExams = value;
                OnPropertyChanged("ListCustomerExams");
            } 
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}
